import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',  // 重定向:重新指向其它path,会改变网址
  },
  {
    path: '/login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/home',
    // 导航守卫 登陆后才能访问
    beforeEnter(to,from,next){ // to 是去哪 from 是从哪来 next是跳转
         console.log(to,"to")
         console.log(from,"from")
        if( !localStorage.getItem("token")) { // 么有登陆态代表着没有登陆
            // window.location.href = "/login"
            next("/login")
        }  
         next() // 放行

    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Home/home'),
    children:[
      {
        path:"/home/test",
        component:()=>import("../views/Home/test")
      },
      {
        path:"/home/testpaper",
        component:()=>import("../views/Home/testpaper")
      },
      {
        path:"/home/achieve",
        component:()=>import("../views/Home/achievement")
      },
    ]
  },
  {
    path:"*", // * 代表着上面都匹配不到就进这个*，
    component:()=>import("../views/notfind")
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
